<?php
/**
 * 员工管理
 *
 * @author 孙晓晔
 * @version $Id: Extra.class.php 1043 2010-04-01 08:04:38Z sunxy $s
 */

require_once(dirname(__FILE__) . '/Info.class.php');

class Extra extends Info {
	
	public $orderby = array(
		'-' => '请选择',		
		'`join`' => '入职日期',
		'`leave`' => '离职日期',
			
		'source' => '应聘途径',
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'dept' => '部门',
		'sex' => '性别',
		'nationality' => '民族',
		'education' => '学历',
		'marriage' => '婚姻',

		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}

	/**
	 * 预处理
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'emp/extra/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}
		
		if ($px == 'source') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['source'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _where($request) {
		$code_s = $request['code_s'];
		$name_s = $request['name_s'];

		$join1_s = $request['join1_s'];
		$join2_s = $request['join2_s'];
		$leave1_s = $request['leave1_s'];
		$leave2_s = $request['leave2_s'];
		$contract1_s = $request['contract1_s'];
		$contract2_s = $request['contract2_s'];
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}

		if (!empty($code_s)) {
			$where .= " AND code like '" . $code_s . "%'";
		}

		if (!empty($name_s)) {
			$where .= " AND name like '" . $name_s . "%'";
		}
		
		if (!empty($join1_s)) {
			$where .= " AND `join` >= '" . $join1_s . "'";
		}

		if (!empty($join2_s)) {
			$where .= " AND `join` <= '" . $join2_s . "'";
		}
		
		if (!empty($leave1_s)) {
			$where .= " AND `leave` >= '" . $leave1_s . "'";
		}
		
		if (!empty($leave2_s)) {
			$where .= " AND `leave` <= '" . $leave2_s . "'";
		}
		
		if (!empty($contract1_s)) {
			$where .= " AND t1 >= '" . $contract1_s . "'";
		}
		
		if (!empty($contract2_s)) {
			$where .= " AND t2 <= '" . $contract2_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];				
				$row = $this->_view(null, $row);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'emp/extra/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		require_once 'Emp.class.php';
		$emp = new Emp();
		$result = $emp->_view($id);
		$controller->assign_by_ref('emp', $result);
		$controller->assign_by_ref('result', $result);
		
		$controller->display($request, 'emp/extra/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$source = $request['source'];
		$join = $request['join'];

		$urgent = $request['urgent'];
		
		// $content = sprintf("\n%s 修改", Name::emp($_SESSION['id']));
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET source = '%s', `join` = '%s', urgent = '%s', content = concat(content, '%s'), modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $source, $join, $urgent, $content, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
		
		$this->findPost($controller, $request);
		return true;
	}

	function nationality($tbl, $name) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE name = '%s'";
		$sql = sprintf($fmt, $tbl, $name);
		// debug($sql);
		$result = $db->query($sql);

		if ($row = $db->fetch_array($result)) {
			return $row['code'];
		}

		return "-";
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-100 day'));
		$controller->assign('t2', strtotime('+1 day'));
	
		$controller->display($request, 'emp/leave/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'dept');

		$controller->assign('groupby', $this->groupby[$px]);
		
		return $this->groupby_dd($px);
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'dept') {
			$row['a'] = Name::dept($row['a']);
		} elseif ($px == 'sex') {
			$row['a'] = $this->sex[$row['a']];
		} elseif ($px == 'nationality') {
			$row['a'] = $this->nationality[$row['a']];
		} elseif ($px == 'education') {
			$row['a'] = $this->education[$row['a']];
		} elseif ($px == 'marriage') {
			$row['a'] = $this->marriage[$row['a']];
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
}
?>
